package com.yurun.controller;

import com.yurun.common.Result;
import com.yurun.entity.Score;
import com.yurun.service.ScoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 宫静雨
 * @since 2022-08-29 15:45:34
 */
@RestController
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @PostMapping("/YR100404")
    public Result add(@RequestBody Score score) {
        return scoreService.save(score) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @PostMapping("/YR100403")
    public Result modify(@RequestBody Score score) {
        if (Objects.isNull(score.getScoreId())) {
            throw new IllegalArgumentException();
        }
        return scoreService.updateById(score) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @GetMapping("/YR100402")
    public Result getById(@RequestParam(name = "scoreId") Long scoreId) {
        return Result.res("000000", "Succeeded")
                .setData(scoreService.getById(scoreId));
    }

    @GetMapping("/YR100401")
    public Result list(@RequestParam(required = false) List<Integer> courseId,
                       @RequestParam(required = false) List<Integer> studentId,
                       @RequestParam(required = false) Integer lowScore,
                       @RequestParam(required = false) Integer highScore) {
        final List<Score> scores = scoreService.listScore(courseId, studentId, lowScore, highScore);
        return Result.res("000000", "Succeeded")
                .setData(scores);
    }
}
